import {
	tempTable
} from './tempTable'
let sqlHelper = {};
sqlHelper.dbName = 'lvyan_app_db_01';
sqlHelper.returnModel = {

	code: 0,
	message: '',
	dbName: '',
	tableName: '',
	sql: '',
	sqlite: null,
	data: null,


};
// code 0 是成功 -1 是失败
/*
初始化数据库
*/
sqlHelper.OpenDB = () => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	return new Promise((resolve, reject) => {
		plus.sqlite.openDatabase({
			name: sqlHelper.dbName,
			path: '_doc/lvyan_app_' + plus.runtime.appid + '.db',
			success: function(e) {
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.message = '创建数据库成功';
				console.log(returnModel);
				resolve(returnModel)
			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.message = '创建数据库失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}
/*
关闭数据库
*/
sqlHelper.CloseDB = () => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	return new Promise((resolve, reject) => {
		plus.sqlite.closeDatabase({
			name: sqlHelper.dbName,
			success: function(e) {
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.message = '关闭数据库成功';
				console.log(returnModel);

				resolve(returnModel)
			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.message = '关闭数据库失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}
/*
创建数据表
*/
sqlHelper.CreateTable = (tableName, columns) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'create table if not exists ' + tableName + '(';
	columns.forEach((column, index) => {
		sql += '"' + column + '" nvarchar(500)'
		if (columns.length - 1 > index) sql += ','
	})
	sql += ')';
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '创建数据表成功';
				console.log(returnModel);
				resolve(returnModel)
			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '创建数据表失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}

/*查看数据 sqlHelper.FindAll('tableName')*/
sqlHelper.FindAll = (tableName) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: sqlHelper.dbName,
			sql: 'select * from ' + tableName,
			success: function(data) {
				returnModel.data = data;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看成功';
				console.log(returnModel);
				resolve(returnModel)

			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}
/*查看*/
sqlHelper.Find = (tableName, keys) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'select * from  ' + tableName + ' where  ' + tempTable.CommanColumn;
	if (typeof(keys) == 'object') {
		sql += ' in (';
		keys.forEach((key, index) => {
			sql += '"' + key + '"';
			if (keys.length - 1 > index) sql += ','
		})
		sql += ')';
	} else {
		sql += ' ="' + keys + '"'
	}
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(data) {
				returnModel.data = data;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看成功';
				sqlHelper.sql = sql;
				console.log(returnModel);
				resolve(returnModel)
			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看失败';
				sqlHelper.sql = sql;
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}

/*
新增数据 sqlHelper.Insert('tableName', [{Id: 1,createdate: '2020-01-2',url: 'http://www.baidu.com'}])
*/
sqlHelper.Insert = (tableName, data) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = "insert into " + tableName + "(";
	let keys = Object.keys(data[0]);
	keys.forEach((key, index) => {
		sql += "[" + key + "]"
		if (keys.length - 1 > index) sql += ','
	})
	sql += ") values";

	data.forEach((rows, index) => {
		sql += "(";
		keys.forEach((key, index) => {
			sql += "'" + rows[key] + "'"
			if (keys.length - 1 > index) sql += ','
		})
		sql += ")";
		if (data.length - 1 > index) sql += ','
	})
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				returnModel.sql = sql;
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据插入成功';
				console.log(returnModel);
				resolve(returnModel)

			},
			fail: function(e) {
				returnModel.sql = sql;
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据插入失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}
/*删除*/
sqlHelper.DeleteAll = (tableName) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'delete from  ' + tableName;
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				returnModel.sql = sql;
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除成功';
				console.log(returnModel);
				resolve(returnModel)

			},
			fail: function(e) {
				returnModel.sql = sql;
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}
/*删除 				return sqlHelper.Delete('tableName',1)  				return sqlHelper.Delete('tableName',[1,2])*/
sqlHelper.Delete = (tableName, keys) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'delete from  ' + tableName + ' where  ' + tempTable.CommanColumn;
	if (typeof(keys) == 'object') {
		sql += ' in (';
		keys.forEach((key, index) => {
			sql += '"' + key + '"';
			if (keys.length - 1 > index) sql += ','
		})
		sql += ')';
	} else {
		sql += ' ="' + keys + '"'
	}
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				returnModel.sql = sql;
				returnModel.data = [];
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除成功';
				console.log(returnModel);
				resolve(returnModel)

			},
			fail: function(e) {
				returnModel.sql = sql;
				returnModel.data = [];
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}

/*
更新
*/
sqlHelper.Update = (tableName, newData) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = '';



	sql += 'delete from  ' + tableName + ' where  ' + tempTable.CommanColumn + ' ="' + newData[tempTable.CommanColumn] +
		'"; '
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				sql = "insert into " + tableName + "(";
				let keys = Object.keys(newData);

				keys.forEach((key, index) => {
					sql += "[" + key + "]"
					if (keys.length - 1 > index) sql += ','
				})
				sql += ") values";
				sql += " (";
				keys.forEach((key, index) => {
					sql += "'" + newData[key] + "'"
					if (keys.length - 1 > index) sql += ','
				})
				sql += ");";

				plus.sqlite.executeSql({
					name: sqlHelper.dbName,
					sql: sql,
					success: function(e) {
						returnModel.sql = sql;
						returnModel.data = [];
						returnModel.sqlite = e;
						returnModel.code = 0;
						returnModel.dbName = sqlHelper.dbName;
						returnModel.tableName = tableName;
						returnModel.message = '更新成功';
						console.log(returnModel);
						resolve(returnModel)
					},
					fail: function(e) {
						returnModel.sql = sql;
						returnModel.data = [];
						returnModel.sqlite = e;
						returnModel.code = -1;
						returnModel.dbName = sqlHelper.dbName;
						returnModel.tableName = tableName;
						returnModel.message = '更新失败';
						console.log(returnModel);
						resolve(returnModel)

					},
				})


			},
			fail: function(e) {
				returnModel.sql = sql;
				returnModel.data = [];
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '更新失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});


}




/*更具外键删除*/
sqlHelper.DeleteForeignKey = (tableName, keys) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'delete from  ' + tableName + ' where  ' + tempTable.CommanColumn + tempTable.CommanColumn_ForeignKey;
	if (typeof(keys) == 'object') {
		sql += ' in (';
		keys.forEach((key, index) => {
			sql += '"' + key + '"';
			if (keys.length - 1 > index) sql += ','
		})
		sql += ')';
	} else {
		sql += ' ="' + keys + '"'
	}
	return new Promise((resolve, reject) => {
		plus.sqlite.executeSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(e) {
				returnModel.sql = sql;
				returnModel.data = [];
				returnModel.sqlite = e;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除成功';
				console.log(returnModel);
				resolve(returnModel)

			},
			fail: function(e) {
				returnModel.sql = sql;
				returnModel.data = [];
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '删除失败';
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}



/*外键查看*/
sqlHelper.FindForeignKey = (tableName, keys) => {
	var returnModel = JSON.parse(JSON.stringify(sqlHelper.returnModel));
	let sql = 'select * from  ' + tableName + ' where  ' + tempTable.CommanColumn + tempTable.CommanColumn_ForeignKey;
	if (typeof(keys) == 'object') {
		sql += ' in (';
		keys.forEach((key, index) => {
			sql += '"' + key + '"';
			if (keys.length - 1 > index) sql += ','
		})
		sql += ')';
	} else {
		sql += ' ="' + keys + '"'
	}
	return new Promise((resolve, reject) => {
		plus.sqlite.selectSql({
			name: sqlHelper.dbName,
			sql: sql,
			success: function(data) {
				returnModel.data = data;
				returnModel.code = 0;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看成功';
				sqlHelper.sql = sql;
				console.log(returnModel);
				resolve(returnModel)
			},
			fail: function(e) {
				returnModel.sqlite = e;
				returnModel.code = -1;
				returnModel.dbName = sqlHelper.dbName;
				returnModel.tableName = tableName;
				returnModel.message = '数据查看失败';
				sqlHelper.sql = sql;
				console.log(returnModel);
				resolve(returnModel)
			}
		});
	});
}


module.exports = {
	sqlHelper
}
